package com.ffox.service.impl;

import com.ffox.entity.PlatformInformation;
import com.ffox.fastDfsUtil.FastDfsUtil;
import com.ffox.mapper.PlatformLoginMapper;
import com.ffox.result.Result;
import com.ffox.service.PlatformLoginService;
import com.ffox.jjwt.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 平台登录服务实现类
 */
@Service
public class PlatformLoginServiceImpl implements PlatformLoginService {
    @Autowired
    PlatformLoginMapper platformLoginMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private FastDfsUtil fastDfsUtil;
    @Autowired
    private HttpServletRequest request;

    /**
     * 平台用户登录验证
     *
     * @param number   平台用户名
     * @param password 平台用户密码
     * @return Result<LoginPlatform> 登录结果，包含成功/失败状态和用户数据
     */
    @Override
    public Result<Map<String, Object>> platformLogin(String number, String password) {
        System.out.println("service层===>逻辑层接收到请求---number:"+number+"---password:"+password);
        // 根据用户名查询用户信息
        PlatformInformation datas = platformLoginMapper.getPlatformByNumber(number);

        if (datas == null) {
            return Result.fail("账号不存在");
        }

        // 验证密码是否正确
        if (!bCryptPasswordEncoder.matches(password, datas.getPassword())) {
            return Result.fail("账号或密码错误");
        }

        // 更新登录时间和IP地址
        platformLoginMapper.updatePlatform(datas.getId(), new HashMap<String, Object>() {{
            System.out.println(LocalDateTime.now());
            put("lastLoginTime", LocalDateTime.now());
            put("lastLoginIp", request.getRemoteAddr());
        }});

        List<String> roles = Arrays.asList("ROLE_PLATFORM");

        // 生成JWT令牌
        String token = JwtUtils.generateToken(number,roles);

        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("name", datas.getNumber());
        userInfo.put("avatar", datas.getAvatar());
        userInfo.put("role", "platform");
        userInfo.put("number", number);

        Map<String, Object> result = new HashMap<>();
        result.put("userInfo", userInfo);
        result.put("token", token);

        System.out.println("service===>返回结果为---"+Result.success("登录成功", result));
        return Result.success("登录成功", result);
    }

    /**
     * 新增平台用户
     *
     * @param number   平台用户名
     * @param password 平台用户密码
     * @param mobile   用户手机号
     * @return Result<Integer> 操作结果，包含成功/失败状态和影响行数
     */
    @Override
    public Result<Integer> platformInsert(String number, String password, String mobile) {
        // 检查用户是否已存在
        if (platformLoginMapper.getPlatformByNumber(number) != null) {
            return Result.fail("用户已存在");
        }

        // 构建用户对象并插入数据库
        PlatformInformation loginPlatform = new PlatformInformation();
        loginPlatform.setNumber(number);
        loginPlatform.setPassword(bCryptPasswordEncoder.encode(password));
        loginPlatform.setMobile(mobile);
        loginPlatform.setAvatar("http://192.168.190.53:80/group1/M00/00/00/wKi-NWiYwqmARlxuAAEikS43N48254.png");
        loginPlatform.setCreatedAt(LocalDateTime.now());
        Integer result = platformLoginMapper.insertPlatform(loginPlatform);
        if (result > 0) {
            return Result.success("用户添加成功", result);
        } else {
            return Result.fail("用户添加失败");
        }
    }

    //    逻辑删除
    @Override
    public Result<Integer> platformDelete(Long id) {
        Integer result = platformLoginMapper.updatePlatform(id, new HashMap<String, Object>() {{
            put("status", 0);
        }});
        if (result == 1) {
            return Result.success("用户删除成功", result);
        } else {
            return Result.fail("用户删除失败");
        }
    }
}
